﻿using System;

using BusinessLogic.ClassFactory;
using IssueCommon;
using Common;
using DataAccess;


namespace RichECMS.ServerPages.Issue
{
    public partial class Issue : Util.BasePage.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDataBind();
            }
        }

        private void InitDataBind()
        {
            using (DBManager _DbManager = new DBManager())
            {
                ArtSecRelationFactory objArtSecRelationFactory = new ArtSecRelationFactory(_DbManager);
                BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objArtSecRelationFactory.CreatorBySql(string.Format("0=0 and intState={0} and intWSID={1}", (int)Utility.ArticleState.NoIssue, State.intWSID));
                if (objArtSecRelation != null)
                {
                    labNoIssueA.Text = objArtSecRelation.Count.ToString();
                    labNoIssueA2.Text = objArtSecRelation.Count.ToString();
                }
                else
                {
                    labNoIssueA.Text = "0";
                    labNoIssueA2.Text = "0";
                }
                //全部的文章数据统计
                objArtSecRelation = objArtSecRelationFactory.CreatorBySql(string.Format("0=0 and intWSID=" + State.intWSID));
                if (objArtSecRelation != null)
                {
                    labIssueA2.Text = objArtSecRelation.Count.ToString();
                }
                else
                {
                    labIssueA2.Text = "0";
                }
                //栏目相关数据统计
                SectionsFactory objSectionsFactory = new SectionsFactory(_DbManager);
                BusinessLogic.BusinessRules.Sections objSections = objSectionsFactory.CreatorBySql(string.Format("0=0 and intIssueState={0} and intWSID={1}", (int)Utility.STIssueState.NoIssue, State.intWSID));
                if (objSections != null)
                {
                    labNoIssueS.Text = objSections.Count.ToString();
                    labNoIssueS2.Text = objSections.Count.ToString();
                }
                else
                {
                    labNoIssueS.Text = "0";
                    labNoIssueS2.Text = "0";
                }

                objSections = objSectionsFactory.CreatorBySql(string.Format("0=0 and intWSID=" + State.intWSID));
                if (objSections != null)
                {
                    labIssueS2.Text = objSections.Count.ToString();
                }
                else
                {
                    labIssueS2.Text = "0";
                }
                objSectionsFactory.Dispose();
                objArtSecRelationFactory.Dispose();
            }

            if (State.LoginUser.ManagerCompetence <= (int)Utility.ManagerCompetence.UserCanIssue)
            {
                rbtnIssueATAll.Enabled = true;
                rbtnIssueSTAll.Enabled = true;
            }
            else
            {
                rbtnIssueATAll.Enabled = false;
                rbtnIssueSTAll.Enabled = false;
            }

        }

        protected void ibtnIssue_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            using (IssueManage objIssueManage = new IssueManage(State.LoginUser))
            {
                objIssueManage.SessionUser = State.LoginUser;
                if (!AppConfig.GetEnabledInculded())
                {
                    if (!AppConfig.GetCommonIssue())
                    {
                        //不生成common
                    }
                    else
                    {
                        objIssueManage.IssueInclude(State.intWSID);
                    }
                }
                else
                {
                    objIssueManage.IssueInclude(State.intWSID);
                }

                objIssueManage.IssueIndex(State.intWSID);
                //栏目--增量发布
                if (rbtnIssueST.Checked)
                {

                    objIssueManage.IssueSection(false, State.intWSID);
                }

                //栏目--重新发布
                if (rbtnIssueSTAll.Checked)
                {
                    objIssueManage.IssueSection(true, State.intWSID);
                }

                //文章--增量发布
                if (rbtnIssueAT.Checked)
                    objIssueManage.IssueArticle(false, State.intWSID);
                //文章--重新发布
                if (rbtnIssueATAll.Checked)
                    objIssueManage.IssueArticle(true, State.intWSID);

                objIssueManage.ClearCaches();
            }

            InitDataBind();

            if (AppConfig.GetIsChangeToHtml() == (int)Utility.IssueWebPageChangeHtml.changetohtml)
            {
                //objIssueManage.IssueInclude();
            }

            Alert("发布成功！");
        }
    }
}